<?php
/***
 * 描述：客户订单页面
 * @category		Mobile		
 * @package			Action			
 * @subpackage		
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: OrderManageAction.class.php 5 2013-10-14 03:20:14Z whtaok@gmail.com $
 */
class OrderManageAction extends Action {
	protected $curr_user_id;
	protected $curr_shop_id;
	/**
	 * 描述：初始化模块
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-4-14
	 */
	protected  function _initialize() {
		ini_set('session.cookie_domain', 'herowind77.vicp.cc');
	
		if(!session('svc_user_id')){
			$this->display('Index/login');
		}else{
			$this->curr_shop_id = session('svc_shop_id');
			$this->curr_user_id = session('svc_user_id');
		}
		
	}
	/**
	 * 描述：订单列表列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$this->display('index');
	}
	/**
	 * 描述：查询登录店员负责的订单列表
	 * 配送中的订单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showMyListAct(){
		$user = session('svc_user');
		$user_id = session('svc_user_id');
		$order_status = I('order_status');
		
		$mod = new CommonModel('OrderLib');
		$modWhere['order_status'] = $order_status;
		if($user['role_name'] == 'saler' || $user['role_name'] == 'sender'){
			$modWhere['_string'] = "shop_saler_id= $user_id OR shop_sender_id= $user_id ";
		}
		$totalRows = $mod->where($modWhere)->count();
		list($list,$page) = $mod->where($modWhere)->listPageMobile($totalRows);
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->display('index');
		
	
	}
	/**
	 * 描述：查询订单详情
	 * 配送中的订单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showDetailAct() {
		$orderData = OrderAdminModel::findDetail($this->curr_shop_id,I('order_code'));
		
		if($orderData){
			//查询店铺信息
			$mod = new ShopUserModel();
			$users = $mod->where(array('shop_id'=>$this->curr_shop_id,'is_valid'=>array('in',array(1,2))))->select();
			$this->assign('orderDetail',$orderData[0]);
			$this->assign('orderItems',$orderData[1]);
			$this->assign('users',$users);
			$this->display('detail');
		}else{
			$this->error('订单不存在');
		}
	}
	
	/**
	 * 更改订单状态为打包状态
	 * 
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doReadyPackageAct(){
		$order_code = I('order_code');
		$upd_time = I('upd_time');
		$shop_saler=explode(',', I('shop_saler'),2);
		if(count($shop_saler)!=2){
			$this->error('请选择负责打包的店员');
			exit();
		}
		$orderData = OrderAdminModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			if($order['order_status']==1 && $order['upd_time']==$upd_time){
				$data = array(
					'shop_saler_id'=>$shop_saler[0],
					'shop_saler_name'=>$shop_saler[1],
					'shop_saler_time'=>time(),
					'order_status'=>2,
					'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
					'upd_time'=>time(),
				);
				$flag = OrderAdminModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
		
	}
	
	/**
	 * 更改订单状态为配送状态
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doReadySendAct(){
		$order_code = I('order_code');
		$upd_time = I('upd_time');
		$shop_sender=explode(',', I('shop_sender'),2);
		if(count($shop_sender)!=2){
			$this->error('请选择负责配送的店员');
			exit();
		}
		$orderData = OrderAdminModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			if($order['order_status']==2 && $order['upd_time']==$upd_time){
				$data = array(
						'shop_sender_id'=>$shop_sender[0],
						'shop_sender_name'=>$shop_sender[1],
						'shop_sender_time'=>time(),
						'order_status'=>3,
						'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
						'upd_time'=>time(),
				);
				$flag = OrderAdminModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
	}

	/**
	 * 更改订单状态为已支付状态，正常支付/部分退货/全部退货/订单取消
	 *
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doReadyPayAct(){
		$order_code = I('order_code');				//订单号
		$upd_time = I('upd_time');					//时间戳
		$cust_order_status = I('cust_order_status');//订单状态
		$orderData = OrderAdminModel::findDetail($this->curr_shop_id,$order_code);
		if($orderData){
			$order = $orderData[0];
			if($order['order_status']!=0 && $order['upd_time']==$upd_time){
				$data = array(
						'order_status'=>4,
						'cust_order_status'=>$cust_order_status,
						'upd_user'=>$this->curr_shop_id.'_'.$this->curr_user_id,
						'upd_time'=>time(),
				);
				//判断更新的状态
				switch($cust_order_status){
					case 0:
						$data['cust_pay_amt'] = $order['order_amt']+$order['order_send_fee'];
						break;
					case 1:
						//部分退货：不做处理
						break;
					case 2:
						//全部退货：设置商品为全部退货状态
						$flag = OrderAdminModel::cancelOrderItemAll($order_code);
						if(!flag){
							$this->error("全部退货操作失败，请重新尝试");
						}
						$data['cust_pay_amt'] = 0;
						break;
					case 3:
						//订单取消：设置商品为全部退货状态
						$flag = OrderAdminModel::cancelOrderItemAll($order_code);
						if(!flag){
							$this->error("订单取消操作失败，请重新尝试");
						}
						$data['cust_pay_amt'] = 0;
						break;
					default:
						$this->error('订单状态不正确');
				}
				$flag = OrderAdminModel::updOrder($this->curr_shop_id,$order_code, $data);
				if($flag){
					$this->success("提交成功",U('showDetail',array('order_code'=>$order_code)));
				}else{
					$this->error("提交失败，请重新尝试");
				}
			}else{
				$this->error('订单状态异常，无法更新');
				exit();
			}
		}else{
			$this->error('订单不存在');
		}
	}	
	
	/**
	 * 描述：订单提交成功页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showOrderSuccessAct(){
		$orderCode = I('order_code');
		$this->assign('order_code',$orderCode);
		$this->display('Cust/Order/result');
	}
}

?>